package red.lixiang.controller;

import red.lixiang.model.BaseInfo;
import red.lixiang.service.ListService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.logging.Logger;

import static red.lixiang.base.APPCONST.INDEX_IMAGE;

@Controller
public class HomeController {

    private    final Logger logger = Logger.getLogger(HomeController.class.getName());


    @Autowired
    private ListService listService;

    /**
     * 进入首页的时候就获取微信的openId，先只考虑微信的情况
     * @param request
     * @return
     */
    @GetMapping("/")
    @ResponseBody
    public String showHome(HttpServletRequest request,
                           HttpServletResponse response,
                           Model model){
        return "OK";
    }

    @GetMapping("/home/getHomeImage")
    @ResponseBody
    public String getHomeImage(HttpServletRequest request,
                           HttpServletResponse response){
        BaseInfo baseInfo = listService.getPromotionProductByType(INDEX_IMAGE);
        Map<String,Object> map = (Map<String, Object>) baseInfo.getObject();
        logger.info("come in home");
        return JSON.toJSONString(map.get("products"));
    }



}
